{extend name="adminblue/base" /} 
{block name="resources"/}
<style>
.modal-infp-style{width:100%;margin:10px auto;}
.modal-infp-style table{width:100%;}
.modal-infp-style table tr td{border: 1px solid #e5e5e5;padding: 10px;}
.ns-main{margin-top: 0;}
</style>
{/block}
{block name="main"}
<table class="mytable">
	<tr>
		<th width="20%" style="text-align: left;">
			<button class="btn-common-delete btn-small" onclick="batchDelete()" style="margin:0 5px 0 0 !important;">批量删除</button>
			<button class="btn-common btn-small" onclick="location.href='{:__URL('ADMIN_MAIN/promotion/adddiscount')}';" style="margin:0 5px 0 0 !important;">添加限时折扣</button>
		</th>
		<th width="10%">
			<input type="text" id = 'search_text' placeholder="请输入活动名称" class="input-common" />
			<input type="button" onclick="searchData()" value="搜索" class="btn-common"/>
		</th>
	</tr>
</table>
<table class="table-class">
	<colgroup>
	<col style="width: 2%;">
	<col style="width: 54%;">
	<col style="width: 8%;">
	<col style="width: 20%;">
	<col style="width: 16%;">
	</colgroup>
	<thead>
		<tr align="center">
			<th><i class="checkbox-common"><input type="checkbox" onclick="CheckAll(this)"></i></th>
			<th align="left">活动名称</th>
			<th>活动状态</th>
			<th>有效时间</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody></tbody>
</table>

<!-- 模态框（Modal） -->
<div class="modal fade hide" id="discountInfo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h3>限时折扣详情</h3>
			</div>
			<div class="modal-body">
				<div class="modal-infp-style">
					<table>
						<tr>
							<td style="width:60px;">活动名称</td>
							<td colspan='3' id="discount_name"></td>
						</tr>
						<tr>
							<td>有效期</td>
							<td colspan='3' id="time"></td>
						</tr>
						<tr>
							<td>活动状态</td>
							<td colspan='3' id="status"></td>
						</tr>
						<tr>
							<td colspan='3'>商品列表</td>
						</tr>
						<tr id="goods_list"></tr>
					</table>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
function LoadingInfo(page_index) {
	var search_text = $("#search_text").val();
	var status = {$status};
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/promotion/getdiscountlist')}",
		data : {
			"page_index" : page_index, "page_size" : $("#showNumber").val(), "search_text" : search_text,"status" : status
		},
		success : function(data) {
			if (data["data"].length > 0) {
				$(".table-class tbody").empty();
				for (var i = 0; i < data["data"].length; i++) {
					var html = '';
					switch(data['data'][i]['status']){
					case 0:
						var status_name = '未发布';
						break;
					case 1:
						var status_name = '正常';
						break;
					case 3:
						var status_name = '关闭';
						break;
					case 4:
						var status_name = '结束';
						break;
					}
					html += '<tr align="center">';
					html += '<td><div class="cell">';
					if(data['data'][i]['status'] == 1){
						html += '<i class="checkbox-common disabled"><input name="sub_use" type="checkbox" value="'+data["data"][i]["discount_id"]+'" disabled></i>';
					}else{
						html += '<i class="checkbox-common"><input name="sub_no_use" type="checkbox"  value="'+data["data"][i]["discount_id"]+'"></i>';
					}
					html += '<td align="left">' + data["data"][i]["discount_name"] + '</td>';;
					
					html += '<td>'+ status_name +'</td>';
					html += '<td>开始时间：' + timeStampTurnTime(data["data"][i]["start_time"]) + '<br/>结束时间：' + timeStampTurnTime(data["data"][i]["end_time"]) + '</td>';
					html += '<td>';
					if(data['data'][i]['status'] == 0){
						html += '<a href="javascript:void(0);"onclick="playingDiscount('+data["data"][i]["discount_id"]+')">上线</a>&nbsp;';
						html += '<a href="'+__URL('ADMIN_MAIN/promotion/updatediscount?discount_id=' + data["data"][i]["discount_id"])+'">编辑</a>&nbsp;';
						html += '<a href="javascript:void(0);"onclick="delDiscount('+data["data"][i]["discount_id"]+')">删除</a>&nbsp;';
					}else if(data['data'][i]['status'] == 1){
						html += '<a href="javascript:void(0);"onclick="closeDiscount('+data["data"][i]["discount_id"]+')">关闭</a>&nbsp;';
					}else if(data['data'][i]['status'] == 3){
						html += '<a href="javascript:void(0);"onclick="delDiscount('+data["data"][i]["discount_id"]+')">删除</a>&nbsp;';
					}else if(data['data'][i]['status'] == 4){
						html += '<a href="javascript:void(0);"onclick="delDiscount('+data["data"][i]["discount_id"]+')">删除</a>&nbsp;';
					}
					html += '<a href="javascript:void(0);"onclick="discountInfo('+data["data"][i]["discount_id"]+')">详情</a>&nbsp;';
					html += '</td>';
					html += '</tr>';
					$(".table-class tbody").append(html);
				}
			} else {
				var html = '<tr align="center"><td colspan="5">暂无符合条件的数据记录</td></tr>';
				$(".table-class tbody").html(html);
			}
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
		}
	});
}

function searchData(){
	LoadingInfo(1);
}


//上线
function playingDiscount(discount_id){
    $.ajax({
        type:"post",
        url : "{:__URL('ADMIN_MAIN/promotion/playingDiscount')}",
        data : { "discount_id" : discount_id.toString() },
        dataType : "json",
        success : function(data) {
            if (data["code"] > 0) {
                showMessage('success', data["message"],"{:__URL('ADMIN_MAIN/promotion/getdiscountlist')}");
            }else{
                showMessage('error', data['message']);
            }
            LoadingInfo(1);
        }

    })
}




//删除限时折扣
function delDiscount(discount_id){
	$.ajax({
		type:"post",
		url : "{:__URL('ADMIN_MAIN/promotion/deldiscount')}",
		data : { "discount_id" : discount_id.toString() },
		dataType : "json",
		success : function(data) {
			if (data["code"] > 0) {
				showMessage('success', data["message"],"{:__URL('ADMIN_MAIN/promotion/getdiscountlist')}");
			}else if(data["code"] == -1){
				showMessage('error', '正在进行中的活动不可删除！');
			}else{
				showMessage('error', data['message']);
			}
		}
	})
}

//关闭正在进行的限时折扣
function closeDiscount(discount_id){
	$.ajax({
		type:"post",
		url : "{:__URL('ADMIN_MAIN/promotion/closediscount')}",
		data : { "discount_id" : discount_id },
		success : function(data) {
			if (data["code"] > 0) {
				showMessage('success', data["message"],"{:__URL('ADMIN_MAIN/promotion/getdiscountlist')}");
			}else{
				showMessage('error', data["message"]);
			}
		}
	})
}

function discountInfo(discount_id){
	$("#discountInfo").modal("show");
	$.ajax({
		type:"get",
		url : "{:__URL('ADMIN_MAIN/promotion/getdiscountdetail')}",
		data : { "discount_id" : discount_id },
		success : function(data) {
			$("#discount_name").text(data["discount_name"]);
			$("#time").text(timeStampTurnTime(data['start_time'])+' 至 '+timeStampTurnTime(data['end_time']));
			switch(data['status']){
				case 0:
					var status_name = '未发布';
					break;
				case 1:
					var status_name = '正常';
					break;
				case 3:
					var status_name = '关闭';
					break;
				case 4:
					var status_name = '结束';
					break;
			}
			$("#status").text(status_name);
			var goods_list = '';
			for(var i=0;i<data['goods_list'].length;i++){
				goods_list += '<tr>';
				goods_list += '<td><img src="'+__IMG(data['goods_list'][i]['picture_info']['pic_cover_micro'])+'"></td>';
				goods_list += '<td>'+data['goods_list'][i]['goods_name']+'</td>';
				goods_list += '<td>'+data['goods_list'][i]['discount']+' 折</td>';
				goods_list += '</tr>';
			}
			$("#discountInfo table tr:gt(4)").remove();
			$("#goods_list").after(goods_list);
		}
	})
}

//全选
function CheckAll(event){
	var checked = event.checked;
	if(checked) $(".table-class tbody input[type = 'checkbox'][name='sub_no_use']").prop("checked",checked).parent().addClass("selected");
	else $(".table-class tbody input[type = 'checkbox'][name='sub_no_use']").prop("checked",checked).parent().removeClass("selected");
}

//批量删除
function batchDelete() {
	var discount_id = new Array();
	$(".table-class tbody input[type='checkbox']:checked").each(function() {
		if (!isNaN($(this).val())) {
			discount_id.push($(this).val());
		}
	});

	if(discount_id.length ==0){
		$( "#dialog" ).dialog({
			buttons: {
				"确定,#0059d6,#fff": function() {
					$(this).dialog('close');
				}
			},
			contentText:"请选择需要操作的记录",
			title:"消息提醒",
		});
		return false;
	}
	delDiscount(discount_id);
}
</script>
{/block}